package com.shop.service.impl;

import com.shop.entity.LoginUser;
import com.shop.entity.Role;
import com.shop.entity.SecurityUserDetails;
import com.shop.mapper.LoginUserMapper;
import com.shop.mapper.RoleMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class MyUserDetailsService implements UserDetailsService {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    LoginUserMapper loginUserMapper;

    @Autowired
    RoleMapper roleMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        logger.info("用户的用户名: {}", username);
//        String password = bCryptPasswordEncoder.encode("123456");

        LoginUser loginUser=loginUserMapper.get(new LoginUser().setUserName(username));

        List<Role> roles=roleMapper.getList(new Role());

//
//        // 封装用户信息，并返回。参数分别是：用户名，密码，用户权限
//        String encode = bCryptPasswordEncoder.encode("123456");
//        //123456   "$2a$10$rE5.RvkHaB06t.9GjGeaW.jNHysRQpBXObl3ZSahzBesfq7tAkX56"
//        SecurityUser  securityUser = new SecurityUser(username, encode,
//                Arrays.asList(new Role().setRoleName("admin")));

        return new SecurityUserDetails(loginUser,roles);


    }

//    private Collection<GrantedAuthority> getAuthorities() {
//        List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();
//        authList.add(new SimpleGrantedAuthority("ROLE_USER"));
//        authList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
//        return authList;
//    }

}
